#include <cstdio>
//#include "AbstractCell.h"
#include <vector>

using namespace std;

class ConwayCell : public AbstractCell {
	
	private:
	/**
	* @param alive bool representing whether cell is alive or not
 	*/
	bool alive;

	public:
	
	/**
 	* Default constructor. Cell is dead by default
 	*/
	ConwayCell():
	alive(false)
	{}
	
	/**
 	* constructor
 	* @param live whether or not cell is alive
 	*/
	ConwayCell(bool live):
	alive(live)
	{}
	
	/**
 	* returns whether the cell is alive or not
	* @return true if alive, false is dead
 	*/
	bool isAlive(){
		return alive;
	}
	

	/**
 	* evolves the cell at (x,y) given the alive neighbor count in neighbors
 	* @param neighbors alive neighbor count. neighbors[0] is ConwayCell neighbors
	* @param x x coordinate of cell
	* @param y y coordinate of cell
	* @return false always. This is used in Cell.h processing.
 	*/
	bool evolve(vector<vector<vector<int> > >& neighbors, int x, int y){
		int n=neighbors[0][x][y];
		if(alive){
			if(n<2||n>3){
				alive=false;
			}
		}
		else{
			if(n==3)
				alive=true;
		}
		return false;
	}



	/**
 	* increments live neighbor count around cell (X,Y). Cell must be alive for this to make sense.
 	* @param neighbors alive neighbor count. neighbors[0] is ConwayCell neighbors
	* @param X x coordinate of cell
	* @param Y y coordinate of cell
 	*/
	//TODO Method isn't elegant....
	void updateNeighborCount(vector<vector<vector<int> > >& neighbors, int X, int Y){
		assert(isAlive());
		vector<vector<int> >* n = &neighbors[0];
		
		for(int x=X-1; x <= X+1; ++x){
			for(int y=Y-1; y <= Y+1; ++y){
				++((*n)[x][y]);
			}
		}
		--((*n)[X][Y]);//can't increment cell's own location
	}
	
	/**
 	* prints the cell
 	*/
	void print(){
		if(alive)
			printf("*");
		else
			printf(".");
	}

	/**
 	* set the state of the cell
	* @param c char that represents the cell's state
 	*/
	void setCell(char c){
		if(c=='.')
			alive=false;
		else
			alive=true;
	}
	
	/**
 	* resets the live neighbor counts for the ConwayCell neighbor grid
 	* @param neighbors alive neighbor count. neighbors[0] is ConwayCell neighbors
 	*/
	static void resetNeighborCount(vector<vector<vector<int> > >& neighbors){
		for(int x=0;x<neighbors[0].size();++x){
			for(int y=0; y<neighbors[0][0].size(); ++y){
				neighbors[0][x][y]=0;	
			}
		}
	}
	
	/**
 	* ConwayCells have no notion of age. Always returns 0
 	* @return 0
 	*/
	int getAge(){
		return 0;
	}

	bool operator == (ConwayCell& c) {
		return alive==c.isAlive();
	}
	
	/**
 	* Returns an integer representation of the cell type.
 	* @return 0
 	*/
	int getType(){
		return 0;
	}


};
